import dayjs from 'dayjs'
//格式化时间
export const formatDate = (dataStr, format = 'YYYY-MM-DD') => {
  if (!dataStr) return ' '
  return dayjs(dataStr).format(format)
}

//格式化金额保留两位小数
export const formatAmount = (amount, decimal = 2, errorText = true) => {
  const floatAmount = parseFloat(amount)
  if (isNaN(floatAmount)) {
    if (errorText) {
      return decimal === 2 ? '0.00' : '0.000'
    }
    return ''
  }
  return floatAmount.toFixed(decimal)
}
//分别取出年龄数值和年龄单位
export const getAgeUnit = (val) => {
  if (!val) return { brAge: '', ageUnit: '' }
  const charsToCheck = ['岁', '月', '天']
  const result = charsToCheck.some((char) => String(val).includes(char))
  if (result) {
    return { brAge: val.substring(0, val.length - 1), ageUnit: val.substring(val.length - 1) }
  } else {
    return { brAge: val, ageUnit: '岁' }
  }
}
//格式化过敏史
export const formatGms = (patientAllergys, gms) => {
  let result = ''
  if (patientAllergys && patientAllergys.length > 0) {
    const selectArr = patientAllergys.filter((item) => {
      return item.status === 2
    })
    if (selectArr && selectArr.length > 0) {
      selectArr.forEach((item, index) => {
        if (selectArr.length === index + 1) {
          result += item.dictItemName + ' (+)'
        } else {
          result += item.dictItemName + ' (+)' + '、'
        }
      })
      gms && (result = result + '、' + gms)
    } else {
      gms && (result = gms)
    }
  } else {
    gms && (result = gms)
  }
  return result
}
